﻿using System.Net;
using System;
using UnityEngine;
using UnityEngine.Events;

namespace GameLogic
{
    public class ClientHandle
    {
        bool m_HasHandShake = false;

        public bool HasHandShake { get => m_HasHandShake; set => m_HasHandShake = value; }
        public UnityAction<ClientHandle> TimeoutCallback { get => m_TimeoutCallback; set => m_TimeoutCallback = value; }

        float m_TimeoutCount = 0;
        UnityAction<ClientHandle> m_TimeoutCallback;
        protected Action<IPEndPoint, byte[], int, int> m_ReceiveCallback;
        public string IP { get; set; }
        public Action<IPEndPoint, byte[], int, int> ReceiveCallback { get => m_ReceiveCallback; set => m_ReceiveCallback = value; }

        public virtual void Send(byte[] data, int length)
        {

        }
        public virtual void Close()
        {

        }
        public virtual void Update()
        {
            if(m_HasHandShake)
            {
                m_TimeoutCount += Time.unscaledDeltaTime;
                if(m_TimeoutCount > NetServer.TimeoutMax)
                {
                    if(m_TimeoutCallback != null)
                        m_TimeoutCallback.Invoke(this);
                }
            }
        }
        public void RecountTimeout()
        {
            m_TimeoutCount = 0;
        }
    }
}
